package mashibing.leetcode;

import java.io.File;
import java.util.Stack;

/**
 * 统计一个文件夹下所有的文件数
 * @author hx
 * @since 2022/11/26 17:42
 */

public class CountFiles {
    public static int getFileNumber(String folderPath) {
        if (null == folderPath || "".equals(folderPath)) {
            return 0;
        }
        File root = new File(folderPath);
        if (!root.isDirectory() && !root.isFile()) {
            return  0;
        }
        if (root.isFile()) {
            return  1;
        }
        Stack<File> stack = new Stack<>();
        stack.add(root);
        int files = 0;
        while (!stack.isEmpty()) {
            File folder = stack.pop();
            for (File next : folder.listFiles()) {
                if (next.isFile()) {
                    files++;
                }
                if (next.isDirectory()) {
                    stack.push(next);
                }
            }
        }
        return files;
    }

    public static void main(String[] args) {
        String path = "/Users/hx/git_project/anti-fake/";
        int fileNumber = getFileNumber(path);
        System.out.println(fileNumber);
    }
}
